package com.errol.batchprocessor.payroll.advisorimpl.online;

import com.renjia.bo.ChannelFeeTypeBO;
import com.renjia.enums.FeeTypeEnum;
import com.renjia.enums.PayChannelEnum;
import com.renjia.service.CoreFeeRecordService;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisorimpl.PayrollAdvisorTypeEnum;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 平安支付扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/17
 * @since v5.0.6
 **/
@Service
public class PingAnAdvisorImpl implements
        PayrollUploadCreateAdvisor {

    private final CoreFeeRecordService coreFeeRecordService;

    public PingAnAdvisorImpl(CoreFeeRecordService coreFeeRecordService) {
        this.coreFeeRecordService = coreFeeRecordService;
    }

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return PayrollAdvisorTypeEnum.OFFLINE != advisorPayload.getPayrollAdvisorTypeEnum();
    }

    @Override
    public int priority() {
        return 131;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        ChannelFeeTypeBO channelFeeTypeBo = new ChannelFeeTypeBO();
        if (PayChannelEnum.PINGAN.getCode().toString().equals(batchPayload.getBatchEntity().getPayType())) {
            channelFeeTypeBo = coreFeeRecordService.getChannelFeeType(batchPayload.getCoreChannelMerchant().getPaParenterId());
            if (channelFeeTypeBo == null) {
                channelFeeTypeBo = new ChannelFeeTypeBO();
                channelFeeTypeBo.setFeeType(FeeTypeEnum.CHANNEL.getCode());
            }
        }
        batchPayload.setChannelFeeTypeBo(channelFeeTypeBo);
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        singlePayload.getBusPayrollRecordDetail().setFeeType(batchPayload.getChannelFeeTypeBo().getFeeType());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        doAfterNewPayrollRecordAtUploadStage(batchPayload);
        return true;
    }
}
